<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/7/12
 * Time: 9:44
 */
namespace app\admin\controller;
use think\File;
use think\Request;
use think\Db;

class ToolController extends CommonController
{
    /**
     * 获取投资工具
     */
    public function getData() {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $key = input('key');
        $map = [];
        if($key != ""){
            $map['tool_name'] = ['like',"%" . $key . "%"];
        }
        $notice = Db::table('ew_tool')
            ->where($map)
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table('ew_tool')
            ->where($map)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $notice;
        return json($data);
    }

    /**
     * 添加页面
     * @return mixed
     */
    public function addTool(){
        return $this->fetch('tool/addtool');
    }

    /**
     * 添加
     * @return \think\response\Json
     */
    public function add(){
        extract(input());
        $param = [];
        if($_FILES['file']['name'][0]!='') {
            $tmp = $_FILES['file']['tmp_name'];
            $names = $_FILES['file']['name'];
            $size = $_FILES['file']['size'];
            $info = $this->uploadFile($tmp,$names,$size);
            if($info['status'] == 200){
                $param['tool_img'] =$info['msg'];
            }else{
                return json(['code'=>100,'msg'=>$info['msg']]);
            }
        }
        $param['tool_name'] = $name;
        $time = date('Y-m-d H:i:s');
        $param['tool_create_time'] = $time;
        $param['tool_update_time'] = $time;
        $res = Db::table('ew_tool')->insert($param);
        if($res){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>100,'msg'=>'添加失败']);
        }
    }

    /**
     * 修改页面
     * @return mixed
     */
    public function updateTool(){
        $id = input('id');
        $res = Db::table('ew_tool')->where('tool_id',$id)->find();
        $this->assign ('res',$res);
        return $this->fetch ('tool/updatetool');
    }

    /**
     * 修改
     * @return \think\response\Json
     */
    public function update(){
        extract(input());
        $param = [];
        if($_FILES['file']['name']!='') {
            $tmp = $_FILES['file']['tmp_name'];
            $names = $_FILES['file']['name'];
            $size = $_FILES['file']['size'];
            $info = $this->uploadFile($tmp,$names,$size);
            if($info['status'] == 200){
                $param['tool_img'] =$info['msg'];
            }else{
                return json(['code'=>100,'msg'=>$info['msg']]);
            }
        }
        $param['tool_name'] = $name;
        $time = date('Y-m-d H:i:s');
        $param['tool_update_time'] = $time;
        $res = Db::table('ew_tool')->where('tool_id',$id)->update($param);
        if($res){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>100,'msg'=>'修改失败']);
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException]
     */
    public function delTool(){
        $id = input('id');
        $res = Db::table('ew_tool')->delete($id);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}